package com.tsd.base.service.impl;

import com.tsd.base.dao.BasePrinterMapper;
import com.tsd.base.entity.BasePrinter;
import com.tsd.base.entity.BasePrinterExt;
import com.tsd.base.service.BasePrinterService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：BasePrinter 服务实现层
 *
 * @author david
 * @date 2022/05/12 09:38:04
 */
@Service
public class BasePrinterServiceImpl extends BaseServiceImpl implements BasePrinterService {

    @Resource
    private BasePrinterMapper basePrinterMapper;

    @Override
    public BasePrinter getBasePrinterById(Long id) throws Exception {
        return basePrinterMapper.selectByPrimaryKey(id);
    }

    @Override
    public BasePrinterExt queryBySid(String sid) throws Exception {
        return basePrinterMapper.selectBySid(sid);
    }

    @Override
    public void deleteBasePrinters(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            BasePrinter old = basePrinterMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            basePrinterMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveBasePrinter(BasePrinter record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getCode(), "请输入编码");
            super.checkEmpty(record.getName(), "请输入名称");
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            basePrinterMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            basePrinterMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<BasePrinterExt> findBasePrinters(Map<String, Object> params, PageBean page) throws Exception {
        return basePrinterMapper.selectByPage(params, page);
    }

    @Override
    public List<BasePrinterExt> findBasePrinters(Map<String, Object> params) throws Exception {
        return basePrinterMapper.selectByParams(params);
    }

    @Override
    public BasePrinterExt getDefaultPrinter() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("dr", 0);
        params.put("state", BasePrinterExt.STATE_HAS_REG3RD);
        List<BasePrinterExt> list = findBasePrinters(params);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
